package taskMgr

import (
	"errors"
	"log"

	"smartconn.cc/tosone/ra_main/common"
)

var tasklist []task
var err error

func init() {

}

// Register 注册一个服务
func Register(name string, channel chan common.ServiceCommand) {
	tasklist = append(tasklist, task{name: name, channel: channel})
	err = Trigger(name)
	if err != nil {
		log.Println(err)
	}
}

// Trigger 触发一个服务
func Trigger(name string) error {
	return trans(name, common.ServiceCommand{Cmd: "start"})
}

// Transport 传输信息到一个服务中
func Transport(name string, info common.ServiceCommand) error {
	return trans(name, info)
}

func trans(name string, info common.ServiceCommand) error {
	for _, t := range tasklist {
		if t.name == name {
			t.channel <- info
			return nil
		}
	}
	return errors.New("No such a services")
}
